﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BBS.Bll.Cache;
using BBS.Bll.Extend;
using BBS.Data.Forum;
using BBS.Entity;
using System.Data;
using Rule = BBS.Entity.Rule;

namespace BBS.Bll.Forum
{
    /// <summary>
    /// 系统基础数据操作类
    /// </summary>
    public static class SystemInfo
    {
        private static readonly RuleTable _ruleTable = new RuleTable();
        private static readonly AccountLevelTable _levelTable = new AccountLevelTable();
      
        /// <summary>
        /// 获取用户等级设定
        /// </summary>
        /// <returns></returns>
        public static List<AccountLevel> GetUserLevelSetting()
        {
            object obj = CacheProvider.GetObject(CacheKey.ForumAccountLevelParameter);
            if (null==obj)
            {
                var dt = _levelTable.GetAll();
                var list = new List<AccountLevel>();
                foreach (DataRow row in dt.Rows)
                {
                    var item = new AccountLevel();
                    item.Id = row["id"].ToInt();
                    item.Points = row["points"].ToInt();
                    item.Title = row["title"].ToString();
                    list.Add(item);
                }
                list.Sort((a, b) => b.Points - a.Points);
                CacheProvider.AddObject(CacheKey.ForumAccountLevelParameter, list);
                return list;
            }
            return (List<AccountLevel>) obj;

        }
        public static void InsertAccountLevel(AccountLevel level)
        {
            _levelTable.Insert(level);
            CacheProvider.Remove(CacheKey.ForumAccountLevelParameter);
        }
        public static void UpdateAccountLevel(AccountLevel level)
        {
            _levelTable.Update(level);
            CacheProvider.Remove(CacheKey.ForumAccountLevelParameter);
        }
        /// <summary>
        /// 获取积分规则参数
        /// </summary>
        /// <returns></returns>
        public static Rule GetForumRuleParameter()
        {
            var obj = CacheProvider.GetObject(CacheKey.ForumRuleParameter);
            if (obj == null)
            {
                var row = _ruleTable.GetTopRule();
                if (null == row)
                {
                    return new Rule();
                }
                var rule = new Rule();
                rule.PostPoints = row["post_points"].ToInt();
                rule.ReplyPoints = row["reply_points"].ToInt();
                rule.MovePoints = row["move_points"].ToInt();
                rule.HighlightPoints = row["highlight_points"].ToInt();
                rule.TopPoints = row["top_points"].ToInt();
                rule.LockPoints = row["lock_points"].ToInt();
                rule.DeletePoints = row["delete_points"].ToInt();
                rule.LoginPoints = row["login_points"].ToInt();
                CacheProvider.AddObject(CacheKey.ForumRuleParameter, rule);
                return rule;
            }
            return (Rule)obj;


        }
        /// <summary>
        /// 插入或更新参数
        /// </summary>
        /// <param name="rule">参数规则实体</param>
        public static void InsertOrUpdateRule(Rule rule)
        {
            if (HaveRuleRecord())
            {
                CacheProvider.Remove(CacheKey.ForumRuleParameter);//移除缓存对象，下次自动获取最新
                _ruleTable.Update(rule);
            }
            else
            {
                _ruleTable.Insert(rule);
            }
        }
        /// <summary>
        /// 数据库中是否有Rule记录
        /// </summary>
        /// <returns></returns>
        private static bool HaveRuleRecord()
        {
            var row = _ruleTable.GetTopRule();
            return row != null;
        }

     
    }
}
